const electron=require('electron');  
const app=electron.app;  


// this should be placed at top of main.js to handle setup events quickly 
if (handleSquirrelEvent(app)) { 
    // squirrel event handled and app will exit in 1000ms, so don't do anything 
   return; 
}



const path=require('path');  
const url=require('url');  
  
const BrowserWindow=electron.BrowserWindow;  
  
// 初始化并准备创建主窗口  
app.on('ready', function () {  
    // 创建一个宽800px 高500px的窗口  
    mainWindow = new BrowserWindow({  
        width: 1200,  
        height: 800,  
    });  
    // 载入应用的inde.html  
    mainWindow.loadURL(url.format({  
        pathname:path.join(__dirname,'/default.html'),  
        protocol:'file:',  
        slashes:true  
    }));  
    // 窗口关闭时触发  
    mainWindow.on('closed', function(){  
        // 想要取消窗口对象的引用， 如果你的应用支持多窗口，你需要将所有的窗口对象存储到一个数组中，然后在这里删除想对应的元素  
        app.quit();          
    });  
    // electron-packager . exe_name --win --out presentSystem --arch=x64 --version=1.0.0 --electron-version=1.4.13 --overwrite --ignore=node_modules --icon=./images/Map_Marker.ico --win32metadata.CompanyName="IMGIS Company Inc." --win32metadata.ProductName="展示系统" --win32metadata.FileDescription="文件介绍"
});  


function handleSquirrelEvent(application) {
    if (process.argv.length === 1) {
        return false;
    }

    const ChildProcess = require('child_process');
    const path = require('path');

    const appFolder = path.resolve(process.execPath, '..');
    const rootAtomFolder = path.resolve(appFolder, '..');
    const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
    const exeName = path.basename(process.execPath);

    const spawn = function(command, args) {
        let spawnedProcess, error;

        try {
            spawnedProcess = ChildProcess.spawn(command, args, {
                detached: true
            });
        } catch (error) {}

        return spawnedProcess;
    };

    const spawnUpdate = function(args) {
        return spawn(updateDotExe, args);
    };

    const squirrelEvent = process.argv[1];
    switch (squirrelEvent) {
        case '--squirrel-install':
        case '--squirrel-updated':
            // Optionally do things such as:
            // - Add your .exe to the PATH
            // - Write to the registry for things like file associations and
            //   explorer context menus

            // Install desktop and start menu shortcuts
            spawnUpdate(['--createShortcut', exeName]);

            setTimeout(application.quit, 1000);
            return true;

        case '--squirrel-uninstall':
            // Undo anything you did in the --squirrel-install and
            // --squirrel-updated handlers

            // Remove desktop and start menu shortcuts
            spawnUpdate(['--removeShortcut', exeName]);

            setTimeout(application.quit, 1000);
            return true;

        case '--squirrel-obsolete':
            // This is called on the outgoing version of your app before
            // we update to the new version - it's the opposite of
            // --squirrel-updated

            application.quit();
            return true;
    }
};
